﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace DFGK_WMS.Repository
{
    internal class TInventorySummaryRepository : BaseRepository<TMaterial>, ITInventorySummaryRepository
    {
        public TInventorySummaryRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }


        /// <summary>
        /// 关键字查询
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="keywords"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<TInventory> GetInventorySummary(out int totalCount, string ? keywords, int pageIndex, int pageSize)
        {

            var list = Context.TInventory.AsQueryable();

            if (keywords != null)
            {
                list = list.Where(x => x.MaterialName.Contains(keywords) || x.MaterialCode.Contains(keywords) || x.OwnerName.Contains(keywords));
            }
           

            totalCount = list.Count();

            list = list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return list.ToList();
        }

        // public List<string> GetMaterialClassification()
        // {
        //    var list = Context.TMaterialCategories.Where(x => x.ParentId == null)
        //        .Select(x => x.Name).ToList();
        //
        //      return list;
        // }

        /// <summary>
        /// 获取物料分类
        /// </summary>
        /// <returns></returns>
        public List<MaterialClassificationDTO> GetMaterialClassification()
        {

            var mlist = Context.TMaterialCategories.ToList();
            var list = from p in mlist
                       select new MaterialClassificationDTO
                       {
                           Name = p.Name,
                           ParentId = p.ParentId,
                           Id = p.Id,
                           Children = GetParentId(p.Id),

                       };


            return list.ToList();
        }

        /// <summary>
        /// 获取物料分类
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        public List<MaterialClassificationDTO> GetParentId(int ParentId)
        {
            

            var mlist = Context.TMaterialCategories.Where(x=>x.ParentId==ParentId).ToList();

            var list = from p in mlist
                       select new MaterialClassificationDTO
                       {
                           Id = p.Id,
                           Name = p.Name,
                           ParentId = p.ParentId,
                           Children = GetParentId(p.Id)
                       };
            return list.ToList();
        }


        /// <summary>
        /// 根据ID查询物料信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<TMaterial> GetMaterialInformationById(int id)
        {
            var list = Context.TMaterial.AsQueryable();

            if (id != null)
            {
                list = list.Where(x => x.MaterialCategoryId == id);
            }

            return list.ToList();
        }


        /// <summary>
        /// 库存汇总导出
        /// </summary>
        /// <returns></returns>
        public List<TInventory> GetSummaryExcel()
        {
            try
            {
                var list = Context.TInventory.AsQueryable();
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取物料总计
        /// </summary>
        /// <param name="MaterialId"></param>
        /// <returns></returns>
        public int GetInventoryQty(int MaterialId)
        {
            var list = Context.TInventory.AsQueryable();
            decimal count = (decimal)list
                 .Where(e => e.MaterialId == MaterialId)
                 .Sum(e => e.AvailableQty);

            int counts = (int)count;
            return counts;
        }

    }
}
